<!DOCTYPE html>
<html>
<head>
<title>Delta-Y Resistor Networks</title>
</head>
<body>
<canvas id="circuitCanvas" width="800" height="450"></canvas>
<script>
const canvas = document.getElementById('circuitCanvas');
const ctx = canvas.getContext('2d');

// Function to draw a resistor with a white background to cover the wire underneath
function drawResistor(x, y, width, height, angle) {
    ctx.save();
    ctx.translate(x, y);
    ctx.rotate(angle);
    ctx.fillStyle = 'white';
    ctx.fillRect(-width / 2, -height / 2, width, height);
    ctx.strokeStyle = 'black';
    ctx.lineWidth = 1.5;
    ctx.strokeRect(-width / 2, -height / 2, width, height);
    ctx.restore();
}

// Function to draw a terminal point
function drawTerminal(x, y) {
    ctx.beginPath();
    ctx.arc(x, y, 4, 0, 2 * Math.PI);
    ctx.fillStyle = 'black';
    ctx.fill();
}

// Clear canvas
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, canvas.width, canvas.height);

ctx.strokeStyle = 'black';
ctx.lineWidth = 1.5;

// --- Delta (Δ) Network ---
const A = { x: 150, y: 100 };
const B = { x: 350, y: 100 };
const C = { x: 250, y: 280 };
const resW = 60;
const resH = 20;

// Draw wires
ctx.beginPath();
ctx.moveTo(A.x, A.y);
ctx.lineTo(B.x, B.y);
ctx.lineTo(C.x, C.y);
ctx.lineTo(A.x, A.y);
ctx.stroke();

// Draw Resistors
// R_AB
let mid_AB = { x: (A.x + B.x) / 2, y: (A.y + B.y) / 2 };
drawResistor(mid_AB.x, mid_AB.y, resW, resH, 0);

// R_BC
let mid_BC = { x: (B.x + C.x) / 2, y: (B.y + C.y) / 2 };
let angle_BC = Math.atan2(C.y - B.y, C.x - B.x);
drawResistor(mid_BC.x, mid_BC.y, resW, resH, angle_BC);

// R_CA
let mid_CA = { x: (C.x + A.x) / 2, y: (C.y + A.y) / 2 };
let angle_CA = Math.atan2(A.y - C.y, A.x - C.x);
drawResistor(mid_CA.x, mid_CA.y, resW, resH, angle_CA);

// Draw Terminals
drawTerminal(A.x, A.y);
drawTerminal(B.x, B.y);
drawTerminal(C.x, C.y);

// Draw Labels
ctx.fillStyle = 'black';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';

// Terminal labels
ctx.font = 'italic 30px Times New Roman';
ctx.fillText('A', A.x - 25, A.y);
ctx.fillText('B', B.x + 25, B.y);
ctx.fillText('C', C.x, C.y + 30);

// Resistor labels
ctx.font = 'italic 24px Times New Roman';
ctx.fillText('R', mid_AB.x, mid_AB.y - 25);
ctx.font = 'italic 16px Times New Roman';
ctx.fillText('AB', mid_AB.x + 12, mid_AB.y - 20);

ctx.font = 'italic 24px Times New Roman';
ctx.fillText('R', mid_CA.x - 45, mid_CA.y + 10);
ctx.font = 'italic 16px Times New Roman';
ctx.fillText('CA', mid_CA.x - 33, mid_CA.y + 15);

ctx.font = 'italic 24px Times New Roman';
ctx.fillText('R', mid_BC.x + 45, mid_BC.y + 10);
ctx.font = 'italic 16px Times New Roman';
ctx.fillText('BC', mid_BC.x + 57, mid_BC.y + 15);


// --- Y (Star) Network ---
const center = { x: 600, y: 190 };
const a = { x: 500, y: 100 };
const b = { x: 700, y: 100 };
const c = { x: 600, y: 280 };

// Draw Wires
ctx.beginPath();
ctx.moveTo(a.x, a.y);
ctx.lineTo(center.x, center.y);
ctx.moveTo(b.x, b.y);
ctx.lineTo(center.x, center.y);
ctx.moveTo(c.x, c.y);
ctx.lineTo(center.x, center.y);
ctx.stroke();

// Draw Resistors
// R_a
let mid_a = { x: (a.x + center.x) / 2, y: (a.y + center.y) / 2 };
let angle_a = Math.atan2(center.y - a.y, center.x - a.x);
drawResistor(mid_a.x, mid_a.y, resW, resH, angle_a);

// R_b
let mid_b = { x: (b.x + center.x) / 2, y: (b.y + center.y) / 2 };
let angle_b = Math.atan2(center.y - b.y, center.x - b.x);
drawResistor(mid_b.x, mid_b.y, resW, resH, angle_b);

// R_c
let mid_c = { x: (c.x + center.x) / 2, y: (c.y + center.y) / 2 };
let angle_c = Math.atan2(center.y - c.y, center.x - c.x); // Should be -PI/2
drawResistor(mid_c.x, mid_c.y, resW, resH, angle_c);

// Draw Terminals
drawTerminal(a.x, a.y);
drawTerminal(b.x, b.y);
drawTerminal(c.x, c.y);

// Draw Labels
ctx.fillStyle = 'black';
ctx.textBaseline = 'middle';

// Terminal labels
ctx.font = '24px Times New Roman';
ctx.textAlign = 'center';
ctx.fillText('a', a.x - 20, a.y);
ctx.fillText('b', b.x + 20, b.y);
ctx.fillText('c', c.x, c.y + 25);

// Resistor labels
// Ra
ctx.font = 'italic 24px Times New Roman';
ctx.textAlign = 'right';
ctx.fillText('R', mid_a.x - 25, mid_a.y);
ctx.font = 'italic 16px Times New Roman';
ctx.textAlign = 'left';
ctx.fillText('a', mid_a.x - 23, mid_a.y + 5);

// Rb
ctx.font = 'italic 24px Times New Roman';
ctx.textAlign = 'left';
ctx.fillText('R', mid_b.x + 25, mid_b.y);
ctx.font = 'italic 16px Times New Roman';
ctx.fillText('b', mid_b.x + 37, mid_b.y + 5);

// Rc
ctx.font = 'italic 24px Times New Roman';
ctx.textAlign = 'left';
ctx.fillText('R', mid_c.x + 20, mid_c.y);
ctx.font = 'italic 16px Times New Roman';
ctx.fillText('c', mid_c.x + 32, mid_c.y + 5);

// Draw Caption
ctx.font = '24px Simsun';
ctx.textAlign = 'center';
ctx.fillText('图 1', canvas.width / 2, 400);

</script>
</body>
</html>